package com.example.steveaoblogdemo.utils;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.steveaoblogdemo.entity.Articles;
import com.example.steveaoblogdemo.mapper.ArticlesMapper;
import com.example.steveaoblogdemo.service.impl.ArticlesServiceImpl;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Aspect
@Component
public class MyAspect {

    @Resource
    private ArticlesMapper articlesMapper;

    @Resource
    private RedisUtil redisUtil;

    //定义一个名为"myPointCut()"的切面，queryById()这个方法中
    @Pointcut("execution(* com.example.steveaoblogdemo.service.impl.ArticlesServiceImpl.queryById(..))")
    public void myPointCut(){}

    //在这个方法执行后
    @After("myPointCut()")
    public void doAfter(JoinPoint joinPoint) throws Throwable {
        Object[] objs=joinPoint.getArgs();
        Integer id=(Integer) objs[0];
        //这个方法的返回值是增加的浏览量个数，不同的文章ID对应的浏览量不一样
        Double view_num = redisUtil.zsIncr("viewNum", String.valueOf(id),1);
        int x = view_num.intValue();
            UpdateWrapper<Articles> wrapper = new UpdateWrapper<>();
            wrapper.eq("id",id).set("view_num",x);
            int result = articlesMapper.update(null,wrapper);
            System.out.println(result);

    }

}
